//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：HDMAssetsHotfix
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
namespace HDMCode.Hotfix
{
    public class HDMAssetsHotfix : HDMAssetsEvent
    {
        /// <summary>
        /// 初始化下载
        /// </summary>
        private void Awake() => CheckAssetsUpdate();

        /// <summary>
        /// 绑定检查资源包错误事件
        /// </summary>
        /// <param name="errorInfo"></param>
        public void ChenckError(string errorInfo) => VerifyErrorEvent?.Invoke(errorInfo);

        /// <summary>
        /// 绑定下载资源包结束事件
        /// </summary>
        /// <param name="isSucceed"></param>
        public void BindDownloadOver(bool isSucceed) => DownloadOverEvent?.Invoke(isSucceed);

        /// <summary>
        /// 绑定下载资源包开始事件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="sizeBytes"></param>
        public void BindDownloadStart(string fileName, long sizeBytes) => DownloadStartEvent?.Invoke(fileName, sizeBytes);

        /// <summary>
        /// 绑定资源包下载错误事件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="errorInfo"></param>
        public void BindDownloadError(string fileName, string errorInfo) => DownloadErrorEvent?.Invoke(fileName, errorInfo);

        /// <summary>
        /// 绑定子资源包下载进度事件
        /// </summary>
        /// <param name="totalDownloadCount"></param>
        /// <param name="currentDownloadCount"></param>
        /// <param name="totalDownloadBytes"></param>
        /// <param name="currentDownloadBytes"></param>
        public void BindDownloadSubProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes) => DownloadSubProgressEvent?.Invoke(totalDownloadCount, currentDownloadCount, totalDownloadBytes, currentDownloadBytes);

        /// <summary>
        /// 绑定主资源包下载进度事件
        /// </summary>
        /// <param name="totalDownloadCount"></param>
        /// <param name="currentDownloadCount"></param>
        /// <param name="totalDownloadBytes"></param>
        /// <param name="currentDownloadBytes"></param>
        public void BindDownDownloadMainProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes) => DownloadMainProgressEvent?.Invoke(totalDownloadCount, currentDownloadCount, totalDownloadBytes, currentDownloadBytes);


        /// <summary>
        /// 平台资源地址绑定
        /// </summary>
        public void PlatformAddress()
        {
#if (UNITY_EDITOR)
            defaultHostServer = DowanerAdderss.TestDefaultHostServer;
            fallbackHostServer = DowanerAdderss.TestFallbackHostServer;
#elif (UNITY_ANDROID)
            defaultHostServer = DowanerAdderss.AndroidDefaultHostServer;
            fallbackHostServer = DowanerAdderss.AndroidFallbackHostServer;
#elif (UNITY_IOS)
            defaultHostServer = DowanerAdderss.IOSDefaultHostServer;
            fallbackHostServer = DowanerAdderss.IOSFallbackHostServer;
#endif
        }

        /// <summary>
        /// 检查资源包
        /// </summary>
        /// <returns></returns>
        public void CheckAssetsUpdate()
        {
            DontDestroyOnLoad(this.gameObject);
            PlatformAddress();
            AssetsInitEvent?.Invoke(this);
            switch (PlayMode)
            {
                case YooAsset.EPlayMode.EditorSimulateMode:

                    StartCoroutine(EditorInitializePackage(assetspackageName, scriptPackageName, SimulationCheckUpdate));
                    break;
                case YooAsset.EPlayMode.OfflinePlayMode:
                    StartCoroutine(OfflineInitializePackage(scriptPackageName, ChenckError, RealCheckUpdate, RealChenckUnUpdate));
                    break;
                case YooAsset.EPlayMode.HostPlayMode:
                    StartCoroutine(InitializePackage(scriptPackageName, ChenckError, RealCheckUpdate, RealChenckUnUpdate));
                    break;
            }
        }

        /// <summary>
        /// 模拟检测更新
        /// </summary>
        /// <param name="mainPackage"></param>
        /// <param name="subPackage"></param>
        public void SimulationCheckUpdate(YooAsset.ResourcePackage mainPackage, YooAsset.ResourcePackage subPackage)
        {
            ScriptPackage = subPackage;
            AssetsPackage = mainPackage;
            HDMAssetsLoad.scriptPackagee = ScriptPackage;
            HDMAssetsLoad.assetsPackage = AssetsPackage;
            NotUpdatedEvent?.Invoke();
        }

        /// <summary>
        /// 实机检测需要更新
        /// </summary>
        /// <param name="downloadInfo"></param>
        public void RealCheckUpdate(AssetsDownloadInfo downloadInfo)
        {
            ScriptPackage = downloadInfo.package;
            StartCoroutine(InitializePackage(assetspackageName, ChenckError,
                (result) =>
                {
                    NeedUpdatedEvent?.Invoke(new AssetsDownloadInfo[] { downloadInfo, result });
                    AssetsPackage = result.package;
                },
                (resultInsid) =>
                {
                    AssetsPackage = resultInsid.package;
                    HDMAssetsLoad.scriptPackagee = ScriptPackage;
                    HDMAssetsLoad.assetsPackage = AssetsPackage;
                    NeedUpdatedEvent?.Invoke(new AssetsDownloadInfo[] { downloadInfo });
                }
                ));
        }

        /// <summary>
        /// 实机检测无需更新
        /// </summary>
        /// <param name="downloadInfo"></param>
        public void RealChenckUnUpdate(AssetsDownloadInfo downloadInfo)
        {
            ScriptPackage = downloadInfo.package;
            StartCoroutine(InitializePackage(assetspackageName, ChenckError,
                (result) =>
                {
                    NeedUpdatedEvent?.Invoke(new AssetsDownloadInfo[] { result });
                    AssetsPackage = result.package;
                },
                (resultInsid) =>
                {
                    AssetsPackage = resultInsid.package;
                    HDMAssetsLoad.scriptPackagee = ScriptPackage;
                    HDMAssetsLoad.assetsPackage = AssetsPackage;
                    NotUpdatedEvent?.Invoke();
                }));
        }

        /// <summary>
        /// 下载单个资源包
        /// </summary>
        /// <param name="packageName">需要下载的资源包</param>
        public void PackageDownload(string packageName)
        {
            StartCoroutine(RunAssetsDownload(packageName, BindDownloadError, BindDownDownloadMainProgress, BindDownloadOver, BindDownloadStart,
                (result) =>
                {
                    HDMAssetsLoad.scriptPackagee = ScriptPackage;
                    HDMAssetsLoad.assetsPackage = AssetsPackage;
                    DownloaderSucceedEvent?.Invoke(result);
                }));
        }

        /// <summary>
        /// 下载合并资源包
        /// </summary>
        /// <param name="mainPackageName">资源包名字</param>
        /// <param name="subPackageName">补丁包名称</param>
        public void PackageDownload(string mainPackageName, string subPackageName)
        {
            if (mainPackageName == string.Empty && subPackageName == string.Empty )
            {
                mainPackageName = this.scriptPackageName;
                subPackageName = this.assetspackageName;
            }

            StartCoroutine(RunAssetsDownloads(mainPackageName, subPackageName, BindDownloadError, BindDownDownloadMainProgress, BindDownloadSubProgress, BindDownloadOver, BindDownloadStart,
                (result) =>
                {
                    HDMAssetsLoad.scriptPackagee = ScriptPackage;
                    HDMAssetsLoad.assetsPackage = AssetsPackage;
                    DownloaderSucceedEvent?.Invoke(result);
                }));
        }

        /// <summary>
        /// 添加元数据到内存
        /// </summary>
        public void AddMateDateToRAM(Action action)
        {
            HDMAssetsLoad.scriptPackagee = ScriptPackage;
            StartCoroutine(LoadMateDate(hotfoxDate, mateDate, action));
        }
    }
}